#!/bin/bash
PANF=/home/cmbpanfs-01/yanghoch # can be overwritten by bashrc
source ~/.bashrc
source ~/SHELL/isFileEmpty.sh
if [ $# -ne 4 ]; then
    echo "This shell generates the HTML only based on the meta files"
    echo ""
    echo "The 1st ptr is gene name"
    echo "The 2nd ptr is chr seq name"
    echo "The 3rd ptr is the range start"
    echo "The 4th ptr is the range end"
    echo "4 ptrs are needed"
    echo "$# is provided" 
    exit 0
fi

# use gene name as working directory
if ! test -d $1; then
    mkdir $1
fi
cd $1
echo "Working in the $PWD"
# create tmp directory 
TMP_DIR='./tmp'
if ! test -d $TMP_DIR; then
    mkdir $TMP_DIR
fi

DATA_DIR=$PANF/'hg18'
SEQ_PATH=$DATA_DIR/"$2.fa"        # The whole sequence
SEQ_OUT=$TMP_DIR/"$1.fa"          # The extracted sequence
SEA_PATH=$DATA_DIR/"$2.sea"       # The whole low coverage regions 
SEA_OUT=$TMP_DIR/"$1.sea"         # The extracted low coverage regions
SAM_PATH=$DATA_DIR/"$2.sam"       # The whole junctions support mappings
SAM_OUT=$TMP_DIR/"$1.sam"         # The extracted detected junctions
SAM2BED_JUC=$TMP_DIR/"$1.bed"     # The extracted knwon junctions
JUC_PATH=$DATA_DIR/"$2.junctions" # The whole known junctions
JUC_OUT=$TMP_DIR/"$1.junctions"   # The extracted known junctions  

READS_DIR=$PANF/'BrainTran/'
#READS_PATH=$READS_DIR/'brain_seq.fastq'
#READS_PATH=$READS_DIR/'brain_seq.txt'

if ! test -s $SAM2BED_JUC ; then
    echo "No detected junction!"
    exit
elif ! test -s $JUC_OUT ; then
    echo "No known junction!" 
fi 
GENE=$(basename $1)
echo "Generate html output base on the meta file ex: .bed or Gene_I.fa "
if test -s $GENE'_I.fa'; then 
    ~/SHELL/outRNAseqHTML.sh $1 $2 $3 $4
else 
    echo "No valid isoform; Skip from HTML report"
fi
exit
